<?php 
class Widget_SeSearch
{
	static function  getConfig(){
		$ret=array();
		$ret['baidu']=array('is_gbk'=>true,'is_dirty'=>false,'url'=>'http://news.baidu.com/ns?word={$query}&tn=newsrss&sr=0&cl=2&rn=20&ct=0&ie=gb2312');
		$ret['google']=array('is_gbk'=>false,'is_dirty'=>false,'url'=>'http://news.google.com/news?ds=n&pq=%E5%8A%A8%E5%9B%BD%E4%BA%8B%E6%95%85&hl=en&cp=2&gs_id=1i&xhr=t&q={$query}&qe=q26LRQ&qesig=2w4GIZVQ_0CzGtatkZUKfg&pkc=AFgZ2tnWWrklUEBJy8tV5ox1dbxU4-cV9JuIXcnmKkT1XIQ8-Attn_CWmhJcaPybI9NSbgx9zdV6fZjAM4R-ML66WCHYZVoHkg&newwindow=1&bav=on.2,or.r_gc.r_pw.&biw=1440&bih=799&um=1&ie=UTF-8&output=rss');
		$ret['bing']=array('is_gbk'=>false,'is_dirty'=>false,'url'=>'http://api.bing.com/rss.aspx?Source=News&Market=zh-CN&Version=2.0&News.SortBy=Date&Query={$query}');
		$ret['soso']=array('is_gbk'=>true,'is_dirty'=>false,'url'=>'http://vdap.soso.com/rss?w={$query}&kd=n&sd=0&nu=20&src=-12&st=t&pid=n.res.sort.t&ty=c');
		$ret['sina']=array('is_gbk'=>true,'is_dirty'=>true,'url'=>'http://rss.sina.com.cn/news/marquee/ddt.xml');
		$ret['youdao']=array('is_gbk'=>true,'is_dirty'=>false,'url'=>'http://news.youdao.com/search?q={$query}+s%3Abytime&doctype=rss');
		return $ret;
	}
	public static function getData($params=array())
	{
		//Baogg_Cache::flush();
		if(!is_array($params) && !isset($params['kw']) || !$params['kw']){
			return array();
		}
		
		$myQuery=$params['kw'];
		$cache_key='widget_se_search_'.md5($myQuery);
		if(($arr_info=Baogg_Cache::get($cache_key))!==false){			
			return $arr_info;
		}

		$arr_info=array();
		$arr_info_source_config=self::getConfig();
		foreach($arr_info_source_config as $v){
			$query=$myQuery;
			isset($v['is_gbk']) && $v['is_gbk'] && ($query=iconv('utf-8','gbk',$query));

			$query=urlencode($query);

			$url=strtr($v['url'],array('{$query}'=>$query));
			//echo $query;echo $url;exit;
			try {
				$slashdotRss = new Zend_Feed_Rss($url);
				//$slashdotRss->setEncoding('gbk');
				isset($v['is_gbk']) && $v['is_gbk'] && ($slashdotRss->setEncoding('gbk'));
			} catch (Zend_Feed_Exception $e) {
				//echo '<pre>';var_dump($slashdotRss);exit; echo $e->getMessage();
				continue;
			}catch(Exception $e2){
				continue;
			}

			//loop get item data
			foreach ($slashdotRss as $item) {
				if(@$v['is_dirty'] && strpos($item->title(),$query)===false && strpos($item->description,$query)===false ){
					continue;
				}
				//addcslashes ,"\\\'\""
				$arr_info[] = array(
							'id'          => count($arr_info)+1, 
		                    'title'       =>$item->title(),
		                   'link'        => $item->link(),
		                    'description' => htmlentities(($item->description()), ENT_QUOTES | ENT_IGNORE, "UTF-8"),
		                    'pubDate'     => date('Y-m-d H:i:s',strtotime($item->pubDate())),
		                    'source'      => $item->source()
				);
			}

		}
		Baogg_Cache::set($cache_key,$arr_info);
		return $arr_info;
	}
	
	public function getJson($params = array()){
		
		$arr_info= self::getData($params);	
			
		$page=@$params['page'];
		$limit=@$params['rows'];
		$sidx=@$params['sidx'];
		$sord=@$params['sord'];
		$count=count((array)$arr_info);
	
		//echo '<pre>';print_r($this->getRequest()->getParams());print_r($options);exit;var_dump($params);exit;
		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
			
		$response=new stdClass();
		$response->page = $page;
		$response->total = $total_pages;
		$response->records = $count;
		/* $response->rows =$arr_info;
		 array();
		foreach($arr_info as $k=>$v){
		$response->rows[]=array('id'=>$k,'rows'=>($v));
		}
		*/
		//echo '<pre>';print_r(array_slice($arr_info, $limit*$page-$limit,$limit));echo $limit;echo $limit*$page-$limit;exit;
		$response->rows=array_slice($arr_info, $limit*$page-$limit,$limit);
	
		return $response;
	}
}

?>